package menu

import (
	"encoding/json"
	"fmt"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"github.com/gin-gonic/gin"
)

// QueryMenuActionList 功能权限列表
// @Summary 功能权限列表
// @Description 功能权限列表
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id query string true "menuId"
// @Success 200 {object} response.TResponse{data=vo.MenuActionVO}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu_action [get]
// @Security LoginToken
func (m *menuController) QueryMenuActionList(c *gin.Context) {
	id, exist := c.GetQuery("id")
	if !exist {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": "id 必传"})
		return
	}
	fmt.Println("id", id)
	actionList, err := m.menuService.QueryMenuActionList(c, id)

	var actionListApi = make(map[string]interface{})
	apiStr, _ := json.Marshal(actionList)
	_ = json.Unmarshal(apiStr, &actionListApi)
	if err == nil {
		response.Success(c, "获取数据成功", actionListApi)
	}
}

// AddMenuAction 创建功能权限
// @Summary 创建功能权限
// @Description 创建功能权限
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param menuId formData string true "menuId"
// @Param method formData string true "请求方法"
// @Param api formData string true "请求地址"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu_action [post]
// @Security LoginToken
func (m *menuController) AddMenuAction(c *gin.Context) {
	var addApiReq QO.AddMenuActionReq
	if err := c.ShouldBind(&addApiReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := m.menuService.AddMenuAction(c, addApiReq)
	if err == nil {
		response.Success(c, "新增数据成功", gin.H{})
	}
}

// DeleteMenuAction 删除功能权限
// @Summary 删除功能权限
// @Description 删除功能权限
// @Tags API.menu
// @Accept json
// @Produce json
// @Param id path string true "hashId"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu_action/{id} [delete]
// @Security LoginToken
func (m *menuController) DeleteMenuAction(c *gin.Context) {
	id := c.Param("id")
	err := m.menuService.DeleteMenuAction(c, id)
	if err == nil {
		response.Success(c, "删除数据成功", gin.H{
			"id": id,
		})
	}
}
